package com.bw.trade.monitor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.core.creditStatus.RefundBorrowStatus;
import com.bw.core.vo.FindOneBankResVo;
import com.bw.trade.fegin.FeginUserClient;
import com.bw.trade.mapper.TbRefoundLogMapper;
import com.bw.trade.pojo.TbRefoundLog;
import com.bw.trade.service.TbRefoundLogService;
import com.bw.trade.vo.PaymentVo;
import com.google.common.collect.Lists;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class MqRepayment {

    @Autowired
    private Snowflake snowflake;
    @Autowired
    private TbRefoundLogMapper refoundLogMapper;
    @Autowired
    private FeginUserClient userClient;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private TbRefoundLogService refoundLogService;

    @RabbitHandler
    @RabbitListener(queues = "item_queue")
    public void getMsg(String msg){

        JSONObject jsonObject = JSONObject.parseObject(msg);
        PaymentVo paymentVo = JSONObject.toJavaObject(jsonObject, PaymentVo.class);
        String orderNo = paymentVo.getMerTradeNo();

        //判断订单是否为空
        if(StrUtil.hasBlank(orderNo)){
            return;
        }
        //根据订单和状态查询是否存在订单

        QueryWrapper<TbRefoundLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo)
                    .eq("borrow_status", Lists.newArrayList(1,2));
        //查询数据是否存在
        List<TbRefoundLog> list = refoundLogMapper.selectList(queryWrapper);
        if(!CollectionUtil.isEmpty(list)){
            return;
        }

        TbRefoundLog tbRefoundLog = new TbRefoundLog();

        tbRefoundLog.setOrderNo(paymentVo.getMerTradeNo());//订单订单编号
        tbRefoundLog.setRefoundStatus(RefundBorrowStatus.REFUND_BORROW_CHECK_RUNNING);//处理状态
        tbRefoundLog.setTransactionNo(snowflake.nextId() + "");
        tbRefoundLog.setIsDelete(0);
        tbRefoundLog.setCreateTime(new Date());
        tbRefoundLog.setUpdateTime(new Date());
        tbRefoundLog.setUserId(paymentVo.getUserId());
        tbRefoundLog.setUserName(paymentVo.getUserName());
        tbRefoundLog.setPayAmount(paymentVo.getAmount());
        //feign调用获取银行卡信息对象
        FindOneBankResVo bank = userClient.findBankResVo(paymentVo.getUserId());
        tbRefoundLog.setRefoundBankName(bank.getBandName());
        tbRefoundLog.setRefoundBankShortName(bank.getBankShortName());
        tbRefoundLog.setRefoundAccount(bank.getBankCardNo());

        //调用添加还款记录的方法
        refoundLogMapper.insert(tbRefoundLog);

        refoundLogService.callMethod(tbRefoundLog,paymentVo);
    }
}
